<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<script>
    $(function () {

        obj1 = {
            editRow : undefined,
            search : function () {
                $('#organizationTypeRuleGrid').datagrid('load', {

                });
            },
            add : function () {
                $('#rulesave,#ruleredo').show();
                /*
                 //当前页行结尾添加
                 $('#box').datagrid('appendRow', {
                 user : 'bnbbs',
                 email : 'bnbbs@163.com',
                 date : '2014-11-11',
                 });
                 */

                if (this.editRow == undefined) {
                    //添加一行
                    $('#organizationTypeRuleGrid').datagrid('insertRow', {
                        index : 0,
                        row : {
                            /*
                             user : 'bnbbs',
                             email : 'bnbbs@163.com',
                             date : '2014-11-11',
                             */
                        },
                    });

                    //将第一行设置为可编辑状态
                    $('#organizationTypeRuleGrid').datagrid('beginEdit', 0);

                    this.editRow = 0;
                }
            },
            save : function () {
                //这两句不应该放这里，应该是保存成功后，再执行
                //$('#save,#redo').hide();
                //this.editRow = false;
                //将第一行设置为结束编辑状态
                $('#organizationTypeRuleGrid').datagrid('endEdit', this.editRow);
            },
            redo : function () {
                $('#rulesave,#ruleredo').hide();
                this.editRow = undefined;
                $('#organizationTypeRuleGrid').datagrid('rejectChanges');
            },
            edit : function () {
                var rows = $('#organizationTypeRuleGrid').datagrid('getSelections');
                if (rows.length == 1) {
                    if (this.editRow != undefined) {
                        $('#organizationTypeRuleGrid').datagrid('endEdit', this.editRow);
                    }

                    if (this.editRow == undefined) {
                        var index = $('#organizationTypeRuleGrid').datagrid('getRowIndex', rows[0]);
                        $('#rulesave,#ruleredo').show();
                        $('#organizationTypeRuleGrid').datagrid('beginEdit', index);
                        this.editRow = index;
                        $('#organizationTypeRuleGrid').datagrid('unselectRow', index);
                    }
                } else {
                    $.messager.alert('警告', '修改必须或只能选择一行！', 'warning');
                }
            },


            remove : function () {
                var rows = $('#organizationTypeRuleGrid').datagrid('getSelections');
                if (rows.length > 0) {
                    $.messager.confirm('确定操作', '您正在要删除所选的记录吗？', function (flag) {
                        if (flag) {
                            var ids = [];
                            for (var i = 0; i < rows.length; i ++) {
                                ids.push(rows[i].id);
                            }
                            //console.log(ids.join(','));
                            $.ajax({
                                type : 'POST',
                                url : '/org/deletetyperule',
                                data : {
                                    ids : ids.join(','),
                                },
                                beforeSend : function () {
                                    $('#organizationTypeRuleGrid').datagrid('loading');
                                    console.log(ids)

                                },
                                success : function (data) {
                                    if (data) {
                                        $('#organizationTypeRuleGrid').datagrid('loaded');
                                        $('#organizationTypeRuleGrid').datagrid('load');
                                        $('#organizationTypeRuleGrid').datagrid('unselectAll');
                                        $.messager.show({
                                            title : '提示',
                                            msg : data + '个用户被删除成功！',
                                        });
                                    }
                                },
                            });
                        }
                    });
                } else {
                    $.messager.alert('提示', '请选择要删除的记录！', 'info');
                }
            },
        };
        $('#organizationTypeRuleGrid').datagrid({
            url : '${path }/org/orgTypeRuleEdit?orgTypeId='+'${typeId}',
            idField : 'id',
            //url : 'content.json',
            title : '组织机构类型列表',
            iconCls : 'icon-search',
            striped : true,
            nowrap : true,
            striped: true,
            rownumbers : true,
            fitColumns : true,
            frozenColumns : [ [ {
                title : 'id',
                field : 'id',
                width : 40,
                hidden : true
            }, {
                field : 'pid',
                title : '上级机构类型',

                hidden : true
            } ,{
                field : 'cid',
                title : '上级机构类型',

                hidden : true
            }] ],
            columns : [[

                {
                    field : 'cOrgType',
                    title : '上级机构类型',
                    sortable : true,

                    editor : {
                        type : 'combobox',
                        options : {
                            required : true,
                            valueField:"id",
                            textField:"name",
                            url:"/org/orgTypelist",
                        },
                    },
                },
                {
                    field : 'num',
                    title : '可设置最大数量',
                    sortable : true,

                    editor : {
                        type : 'validatebox',
                        options : {
                            required : true,
                        },
                    }
                },

            ]],
            toolbar : '#Toolbar',
            pagination : true,
            pageSize : 10,
            pageList : [10, 20, 30],
            pageNumber : 1,
            onAfterEdit : function (rowIndex, rowData, changes) {
                $('#rulesave,#ruleredo').hide();

                var inserted = $('#organizationTypeRuleGrid').datagrid('getChanges', 'inserted');
                var updated = $('#organizationTypeRuleGrid').datagrid('getChanges', 'updated');
                var orgTypeRule;
                var url = info =  '';

                //新增用户
                if (inserted.length > 0) {
                    url = '/org/addtyperule';
                    info = '新增';
                    orgTypeRule ={"id":rowData.id,"pid":'${typeId}',"cid":rowData.cOrgType,"num":rowData.num};
                }

                //修改用户
                if (updated.length > 0) {
                    url = '/org/updatetyperule';
                    info = '修改';
                    orgTypeRule ={"id":rowData.id,"pid":rowData.pid,"cid":rowData.cOrgType,"num":rowData.num};
                }

                $.ajax({
                    type : 'POST',
                    url : url,
                    data :JSON.stringify(orgTypeRule),
                    dataType:"json",
                    contentType:"application/json",
                    beforeSend : function () {
                        $('#organizationTypeRuleGrid').datagrid('loading');
                        console.log(rowData);

                    },
                    success : function (data) {
                        if (data) {
                            $('#organizationTypeRuleGrid').datagrid('loaded');
                            $('#organizationTypeRuleGrid').datagrid('load');
                            $('#organizationTypeRuleGrid').datagrid('unselectAll');
                            $.messager.show({
                                title : '提示',
                                msg : '数据' + info + '成功！',
                                style:{
                                    right:'',
                                    top:document.body.scrollTop+document.documentElement.scrollTop,
                                    bottom:''
                                }

                            });
                            obj.editRow = undefined;
                        }
                    },
                });
                //console.log(rowData);
            },
        })
    });
</script>

<div class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center',border:false"  style="overflow: hidden;">
        <table id="organizationTypeRuleGrid"></table>
    </div>
    <div id="Toolbar" style="display: none;">
        <div style="margin-bottom:5px;">
            <shiro:hasPermission name="+org+8">
                <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="obj1.add();">添加</a>
            </shiro:hasPermission>
            <shiro:hasPermission name="+org+4">
                <a href="#" id="edit" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="obj1.edit();">修改</a>
            </shiro:hasPermission>
            <shiro:hasPermission name="+org+4">
                <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="obj1.remove();">删除</a>
            </shiro:hasPermission>
            <a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" style="display:none;" id="rulesave" onclick="obj1.save();">保存</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-redo" plain="true" style="display:none;" id="ruleredo" onclick="obj1.redo();">取消编辑</a>

        </div>
    </div>
</div>
